/*
 * Copyright (c) 2023 iQi.Co.Ltd. All rights reserved.
 */
 
using System.Collections.Generic;
using NetProtocol.POD;
using NetProtocol.Enum;
using IQIGame.Onigao.Game;
using IQIGame.Onigao.GamePlay;
using IQIGame.Onigao.Framework;
using System.Threading.Tasks;
using System;

namespace NetProtocol.Server {
	/// <summary>
	/// 中心服排行模块
	/// module : centerRank
	/// </summary>
	public interface SNetCenterRank : INetServerModule {
		Dictionary<int, MessageHandler> INetServerModule.GetModuleFunction()
        {	
            return new()
            {
                { NetMessageId.centerRank_getRank, (session, data) =>
		            {
#if ENABLE_NET_LOG || UNITY_EDITOR
						NetProtocolHelper.ServerLog(NetMessageId.centerRank_getRank, "Recv->" + data.ToString());
#endif
		                GetRankRequest _data = (GetRankRequest)data;
						return CS_getRank(session, _data.rankId, _data.count, _data.needUserData);
		            } 
            	},
                { NetMessageId.centerRank_getRankUserData, (session, data) =>
		            {
#if ENABLE_NET_LOG || UNITY_EDITOR
						NetProtocolHelper.ServerLog(NetMessageId.centerRank_getRankUserData, "Recv->" + data.ToString());
#endif
		                GetRankUserDataRequest _data = (GetRankUserDataRequest)data;
						return CS_getRankUserData(session, _data.rankId, _data.lastRanking, _data.rank);
		            } 
            	},
                { NetMessageId.centerRank_getRankGoalie, (session, data) =>
		            {
#if ENABLE_NET_LOG || UNITY_EDITOR
						NetProtocolHelper.ServerLog(NetMessageId.centerRank_getRankGoalie, "Recv->" + data.ToString());
#endif
		                GetRankGoalieRequest _data = (GetRankGoalieRequest)data;
						return CS_getRankGoalie(session, _data.rankId, _data.rankType, _data.ranking);
		            } 
            	},
            };
        }
        
        Dictionary<int, Func<IReceiveMessage>> INetServerModule.GetModuleRequest() 
        {
	        return new ()
	        {
	        	{NetMessageId.centerRank_getRank, ()=> new GetRankRequest()},
	        	{NetMessageId.centerRank_getRankUserData, ()=> new GetRankUserDataRequest()},
	        	{NetMessageId.centerRank_getRankGoalie, ()=> new GetRankGoalieRequest()},
	        };
	    }
        
#region Server-side Interface
		/// <summary>
        /// rankId(1:世界boss)
        /// </summary>
	    protected GetRankResult CS_getRank(ISession session, int rankId, int count, bool needUserData);
		/// <summary>
        /// rankId(1:世界boss)
        /// </summary>
	    protected GetRankUserDataResult CS_getRankUserData(ISession session, int rankId, bool lastRanking, int rank);
		/// <summary>
        /// rankId(1:世界boss)
        /// </summary>
	    protected GetRankGoalieResult CS_getRankGoalie(ISession session, int rankId, int rankType, int ranking);
#endregion
#region Client-side Interface
		/// <summary>
        /// myRanking(-1未参与),rankType(1精确排名,2百分比排名)
        /// </summary>
        public GetRankResult SC_getRankResult(Task<GetRankResult> future){return new GetRankResult(future);}
        public GetRankResult SC_getRankResult(int code, int rankId, bool isOpen, List<RankingPOD> rankings, List<RankingPOD> lastRankings, int myRanking, long myScore, string customData, int rankType){return new GetRankResult(code, rankId, isOpen, rankings, lastRankings, myRanking, myScore, customData, rankType);}
        public GetRankResult SC_getRankResult(int code){return new GetRankResult(code);}
		/// <summary>
        /// 
        /// </summary>
        public GetRankUserDataResult SC_getRankUserDataResult(Task<GetRankUserDataResult> future){return new GetRankUserDataResult(future);}
        public GetRankUserDataResult SC_getRankUserDataResult(int code, string userData){return new GetRankUserDataResult(code, userData);}
        public GetRankUserDataResult SC_getRankUserDataResult(int code){return new GetRankUserDataResult(code);}
		/// <summary>
        /// 
        /// </summary>
        public GetRankGoalieResult SC_getRankGoalieResult(Task<GetRankGoalieResult> future){return new GetRankGoalieResult(future);}
        public GetRankGoalieResult SC_getRankGoalieResult(int code, RankingPOD rankingPOD, int ranking, int rankType){return new GetRankGoalieResult(code, rankingPOD, ranking, rankType);}
        public GetRankGoalieResult SC_getRankGoalieResult(int code){return new GetRankGoalieResult(code);}
#endregion

#region Messages
	public class GetRankResult : MessageResult {
	    private int rankId;
	    private bool isOpen;
	    private List<RankingPOD> rankings;
	    private List<RankingPOD> lastRankings;
	    private int myRanking;
	    private long myScore;
	    private string customData;
	    private int rankType;
	    public GetRankResult(Task<GetRankResult> future): base(future) { }
		public GetRankResult(int code): base(code) { }
		public new GetRankResult SetDelayCall(Action delayCall) { base.SetDelayCall(delayCall); return this; }
	    internal GetRankResult(int code, int rankId, bool isOpen, List<RankingPOD> rankings, List<RankingPOD> lastRankings, int myRanking, long myScore, string customData, int rankType){
	        this.code = code;
	        this.rankId = rankId;
	        this.isOpen = isOpen;
	        this.rankings = rankings == null ? null : new (rankings);
	        this.lastRankings = lastRankings == null ? null : new (lastRankings);
	        this.myRanking = myRanking;
	        this.myScore = myScore;
	        this.customData = customData;
	        this.rankType = rankType;
	    }
        public override MessageResult GetFutureResult(Task task) { return ((Task<GetRankResult>)task).Result; }
		public override int GetMessageId() {return NetMessageId.centerRank_getRankResult;}
		public override SendPackage ToPackage() {
			SendPackage si_p = NetProtocolHelper.GetSendPackage();
			si_p.Header.Messagid = NetMessageId.centerRank_getRankResult;
			si_p.StartWrite();
			SOutputStream _out = si_p.OutputStream;
			_out.Write_int(code);
			_out.Write_int(rankId);
			_out.Write_boolean(isOpen);
			_out.Write_list_pod(rankings);
			_out.Write_list_pod(lastRankings);
			_out.Write_int(myRanking);
			_out.Write_long(myScore);
			_out.Write_string(customData);
			_out.Write_int(rankType);
			return si_p;
		}
		public override string ToString() {
    		return "CenterRank.SC_getRankResult { "
			+ "code="
			+ code
			+ ", rankId="
			+ rankId
			+ ", isOpen="
			+ isOpen
			+ ", rankings="
			+ rankings.ToString<RankingPOD>()
			+ ", lastRankings="
			+ lastRankings.ToString<RankingPOD>()
			+ ", myRanking="
			+ myRanking
			+ ", myScore="
			+ myScore
			+ ", customData="
			+ customData
			+ ", rankType="
			+ rankType
			+" }";
		}
	}
	public class GetRankUserDataResult : MessageResult {
	    private string userData;
	    public GetRankUserDataResult(Task<GetRankUserDataResult> future): base(future) { }
		public GetRankUserDataResult(int code): base(code) { }
		public new GetRankUserDataResult SetDelayCall(Action delayCall) { base.SetDelayCall(delayCall); return this; }
	    internal GetRankUserDataResult(int code, string userData){
	        this.code = code;
	        this.userData = userData;
	    }
        public override MessageResult GetFutureResult(Task task) { return ((Task<GetRankUserDataResult>)task).Result; }
		public override int GetMessageId() {return NetMessageId.centerRank_getRankUserDataResult;}
		public override SendPackage ToPackage() {
			SendPackage si_p = NetProtocolHelper.GetSendPackage();
			si_p.Header.Messagid = NetMessageId.centerRank_getRankUserDataResult;
			si_p.StartWrite();
			SOutputStream _out = si_p.OutputStream;
			_out.Write_int(code);
			_out.Write_string(userData);
			return si_p;
		}
		public override string ToString() {
    		return "CenterRank.SC_getRankUserDataResult { "
			+ "code="
			+ code
			+ ", userData="
			+ userData
			+" }";
		}
	}
	public class GetRankGoalieResult : MessageResult {
	    private RankingPOD rankingPOD;
	    private int ranking;
	    private int rankType;
	    public GetRankGoalieResult(Task<GetRankGoalieResult> future): base(future) { }
		public GetRankGoalieResult(int code): base(code) { }
		public new GetRankGoalieResult SetDelayCall(Action delayCall) { base.SetDelayCall(delayCall); return this; }
	    internal GetRankGoalieResult(int code, RankingPOD rankingPOD, int ranking, int rankType){
	        this.code = code;
	        this.rankingPOD = rankingPOD;
	        this.ranking = ranking;
	        this.rankType = rankType;
	    }
        public override MessageResult GetFutureResult(Task task) { return ((Task<GetRankGoalieResult>)task).Result; }
		public override int GetMessageId() {return NetMessageId.centerRank_getRankGoalieResult;}
		public override SendPackage ToPackage() {
			SendPackage si_p = NetProtocolHelper.GetSendPackage();
			si_p.Header.Messagid = NetMessageId.centerRank_getRankGoalieResult;
			si_p.StartWrite();
			SOutputStream _out = si_p.OutputStream;
			_out.Write_int(code);
			_out.Write_pod(rankingPOD);
			_out.Write_int(ranking);
			_out.Write_int(rankType);
			return si_p;
		}
		public override string ToString() {
    		return "CenterRank.SC_getRankGoalieResult { "
			+ "code="
			+ code
			+ ", rankingPOD="
			+ rankingPOD
			+ ", ranking="
			+ ranking
			+ ", rankType="
			+ rankType
			+" }";
		}
	}
	private class GetRankRequest : IReceiveMessage
    {
		internal int rankId;
		internal int count;
		internal bool needUserData;
        public void FromPackage(ReceivePackage package)
		{
			SInputStream _in = package.InputStream;
            rankId = _in.HasRemaining() ? _in.Read_int() : default;
            count = _in.HasRemaining() ? _in.Read_int() : default;
            needUserData = _in.HasRemaining() ? _in.Read_boolean() : default;
		}
        public int GetMessageId() { return NetMessageId.centerRank_getRank; }
        public override string ToString()
        {
            return "CenterRank.getRank"+ ", rankId="+ rankId+ ", count="+ count+ ", needUserData="+ needUserData;
        }
    }
	private class GetRankUserDataRequest : IReceiveMessage
    {
		internal int rankId;
		internal bool lastRanking;
		internal int rank;
        public void FromPackage(ReceivePackage package)
		{
			SInputStream _in = package.InputStream;
            rankId = _in.HasRemaining() ? _in.Read_int() : default;
            lastRanking = _in.HasRemaining() ? _in.Read_boolean() : default;
            rank = _in.HasRemaining() ? _in.Read_int() : default;
		}
        public int GetMessageId() { return NetMessageId.centerRank_getRankUserData; }
        public override string ToString()
        {
            return "CenterRank.getRankUserData"+ ", rankId="+ rankId+ ", lastRanking="+ lastRanking+ ", rank="+ rank;
        }
    }
	private class GetRankGoalieRequest : IReceiveMessage
    {
		internal int rankId;
		internal int rankType;
		internal int ranking;
        public void FromPackage(ReceivePackage package)
		{
			SInputStream _in = package.InputStream;
            rankId = _in.HasRemaining() ? _in.Read_int() : default;
            rankType = _in.HasRemaining() ? _in.Read_int() : default;
            ranking = _in.HasRemaining() ? _in.Read_int() : default;
		}
        public int GetMessageId() { return NetMessageId.centerRank_getRankGoalie; }
        public override string ToString()
        {
            return "CenterRank.getRankGoalie"+ ", rankId="+ rankId+ ", rankType="+ rankType+ ", ranking="+ ranking;
        }
    }
	}
#endregion
}